package com.game.logic.drawCards;

import com.coment.cfg.luban.PoolCfg;
import com.coment.cfg.luban.Tables;
import com.coment.game.config.ConfigException;
import com.game.logic.core.comment.Bonus;
import com.game.logic.core.comment.BonusPoll;
import com.game.logic.items.bonus.BonusService;
import com.xs.tool.formula.DrawCardsFormula;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/// 抽卡服务{关卡掉落|卡包}
@Slf4j
public class DrawCardsServiceImpl {
    /// 卡池
    public static final DrawCardsFormula<PoolCfg> cardPoolCfg;

    /// 礼包池-关卡掉落
    public static final Map<Integer, BonusPoll<Integer>> levelDropPoll;

    static {
        cardPoolCfg = initializeCardPools();
        log.info("\ud83d\udcc4 加载到 {} 个抽卡池", cardPoolCfg.size());

        levelDropPoll = initializeLevelDropPools();
        log.info("\ud83d\udcc4 加载到 {} 个关卡掉落配置", levelDropPoll.size());
    }

    private static Map<Integer, BonusPoll<Integer>> initializeLevelDropPools() {
        return Tables.cfg.get_tbleveldropcfg().getDataList().stream()
                .collect(Collectors.toMap(
                        k -> k.id,
                        cfg -> {
                            Map<Bonus<Integer>, Integer> map = new HashMap<>();
                            cfg.gift.forEach((k, v) -> {
                                Bonus<Integer> bonus = null;
                                try {
                                    bonus = BonusService.bonusPoll.get(k);
                                } catch (ConfigException e) {
                                    log.error("礼包配置错误");
                                    return;
                                }

                                map.put(bonus, v);
                            });
                            DrawCardsFormula<Bonus<Integer>> bonusDrawCardsFormula = DrawCardsFormula.fromIntegerMap(map);
                            return new BonusPoll<>(bonusDrawCardsFormula, cfg.giftValue);
                        }
                ));
    }

    private static DrawCardsFormula<PoolCfg> initializeCardPools() {
        List<PoolCfg> allCardConfigs = Tables.cfg.get_tbpoolcfg().getDataList();
        log.info("\ud83d\udcc4 加载到 {} 个卡片奖池配置", allCardConfigs.size());


        List<PoolCfg> validCardConfigs = filterValidConfigs(allCardConfigs);
        log.info("✅ 过滤后有效配置 {} 个", validCardConfigs.size());
        return createDrawFormulasForPools(validCardConfigs);
    }
    private static DrawCardsFormula<PoolCfg> createDrawFormulasForPools(List<PoolCfg> configList) {
        Map<PoolCfg, Integer> weightMap = configList.stream().collect(Collectors.toMap(
                (config) -> config,
                (config) -> config.weight,
                Integer::sum,
                HashMap::new
        ));
        return DrawCardsFormula.fromIntegerMap(weightMap);
    }

    private static List<PoolCfg> filterValidConfigs(List<PoolCfg> configs) {
        List<PoolCfg> validConfigs = configs.stream().filter((config) -> config.weight > 0).toList();
        int invalidCount = configs.size() - validConfigs.size();
        if (invalidCount > 0) {
            log.warn("⚠️ 发现 {} 个权重为0的无效配置，已自动过滤", invalidCount);
        }
        return validConfigs;
    }
}
